/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.cron.job.qrcode;

/**
 *
 * @author Marco Berri marcoberri@gmail.com
 */
import com.utils.Default;
import com.utils.HttpUtil;
import com.utils.IOUtil;
import com.utils.StringUtil;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.quartz.*;

/**
 * statistiche: esegue il conteggio della view totali per singolo url
 * 
 * @author marco
 */
public class GenerateAll extends com.cron.job.Base {

    /**
     *
     * @param context
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {


        super.execute(context);
        
        debug("Start execute job " + this.getClass().getName());

        String proxy = "";

        if (!StringUtil.isNullOrEmpty(conf.getProxy().getUrl())) {
            proxy = proxy.replaceAll(conf.getProxy().getUrl(), Default.toString(conf.getProxy().getTo(), "/"));
        }


        if (!StringUtil.isNullOrEmpty(conf.getProxy().getDomain())) {
            proxy = conf.getProxy().getDomain() + "/" + proxy;
        }


        debug("Start execute Job GenerateAll");
        if (this.conf.getQrcode() == null) {
            fatal("qrcode non configurato");
            return;
        }

        File dest = new File(this.path_app_root + "/" + this.conf.getQrcode().getStorage() + "/");

        try {
            org.apache.commons.io.FileUtils.forceMkdir(dest);
        } catch (IOException ex) {
        }


        try {

            Statement statement = this.conn_url.createStatement();
            ResultSet rsstats = statement.executeQuery("select * from url");


            while (rsstats.next()) {
                String id = rsstats.getString("id");
                //se esiste il filoe continue
                File check_file = new File(dest + "/" + id + ".png");
                if (check_file.exists()) {
                    continue;
                }

                String short_url = IOUtil.normalizedPath(proxy + "/r/" + id);
                //da sistemare questa porcata....
                short_url = short_url.replaceAll("http:/", "http://");

                HttpUtil.downloadData(this.conf.getQrcode().getUrl() + "?cht=qr&chs=" + this.conf.getQrcode().getDim() + "&chl=" + short_url, dest.toString() + "/" + id + ".png");

            }

            statement.close();

        } catch (SQLException ex) {
            fatal("SQLException", ex);
        }


        debug("End execute job " + this.getClass().getName());
    }
}
